<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="宠物名称" prop="petName">
        <el-input v-model="queryParams.petName" placeholder="请输入宠物名称" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="领养人" prop="adopter">
        <el-input v-model="queryParams.adopter" placeholder="请输入领养人" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 数据表格 -->
    <el-table v-loading="loading" :data="adoptList">
      <el-table-column label="领养编号" align="center" prop="adoptionId"/>
      <el-table-column label="宠物名称" align="center" prop="petName"/>
      <el-table-column label="宠物图片" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 80px; height: 80px"
            :src="scope.row.image"
            :preview-src-list="[scope.row.image]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="宠物类型" align="center" prop="categoryName"/>
      <el-table-column label="领养人" align="center" prop="userName"/>
      <el-table-column label="联系电话" align="center" prop="phone"/>
      <el-table-column label="领养理由" align="center" prop="reason" :show-overflow-tooltip="true"/>
      <el-table-column label="申请时间" align="center" prop="applyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="领养时间" align="center" prop="approveTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.approveTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-check" @click="handleReview(scope.row)" v-if="scope.row.status === '0'">审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList"/>

    <!-- 查看对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="宠物图片">
              <el-image
                style="width: 200px; height: 200px"
                :src="form.image"
                :preview-src-list="[form.image]">
              </el-image>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="宠物编号">{{ form.petId }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="宠物名称">{{ form.petName }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="宠物类型">{{ form.categoryName }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="宠物年龄">{{ form.age }}岁</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="宠物性别">{{ form.gender === '0' ? '公' : '母' }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="领养人">{{ form.userName }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话">{{ form.phone }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="居住地址">{{ form.address }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="领养理由">{{ form.reason }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请时间">{{ parseTime(form.applyTime) }}</el-form-item>
          </el-col>
          <el-col :span="24" v-if="form.status !== '0'">
            <el-form-item label="审核意见">{{ form.comment }}</el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.status !== '0'">
            <el-form-item label="审核时间">{{ parseTime(form.approveTime) }}</el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.status !== '0'">
            <el-form-item label="审核人">{{ form.approveBy }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 审核对话框 -->
    <el-dialog title="审核领养申请" :visible.sync="reviewOpen" width="500px" append-to-body>
      <el-form ref="reviewForm" :model="reviewForm" :rules="reviewRules" label-width="80px">
        <el-form-item label="审核结果" prop="status">
          <el-radio-group v-model="reviewForm.status">
            <el-radio label="1">通过</el-radio>
            <el-radio label="2">拒绝</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核意见" prop="comment">
          <el-input type="textarea" v-model="reviewForm.comment" placeholder="请输入审核意见"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitReview">确 定</el-button>
        <el-button @click="reviewOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAdopt, getAdopt, reviewAdopt } from "@/api/admin/adopt";

export default {
  name: "AdoptList",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 领养列表
      adoptList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示审核弹出层
      reviewOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        petName: null,
        adopter: null,
        status: null
      },
      // 表单参数
      form: {},
      // 审核表单参数
      reviewForm: {
        adoptId: null,
        status: '1',
        comment: ''
      },
      // 审核表单校验
      reviewRules: {
        status: [
          { required: true, message: "请选择审核结果", trigger: "change" }
        ],
        comment: [
          { required: true, message: "请输入审核意见", trigger: "blur" }
        ]
      },
      // 状态选项
      statusOptions: [
        { value: "0", label: "待审核" },
        { value: "1", label: "已通过" },
        { value: "2", label: "已拒绝" }
      ]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询领养列表 */
    getList() {
      this.loading = true;
      listAdopt(this.queryParams).then(response => {
        this.adoptList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      getAdopt(row.adoptionId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看领养信息";
      });
    },
    /** 审核按钮操作 */
    handleReview(row) {
      this.reviewForm = {
        adoptId: row.adoptionId,
        status: '1',
        comment: ''
      };
      this.reviewOpen = true;
    },
    /** 提交审核 */
    submitReview() {
      this.$refs["reviewForm"].validate(valid => {
        if (valid) {
          reviewAdopt(this.reviewForm).then(response => {
            this.$modal.msgSuccess("审核成功");
            this.reviewOpen = false;
            this.getList();
          });
        }
      });
    }
  }
};
</script>

<style scoped>
.el-table .success-row {
  background: #f0f9eb;
}
</style>
